<template>
  <div class="login-container flex">
    <!-- <img
      src="../../../public/img/login-left.png"
      class="login-left"
    /> -->
    <div class="fz-20 text-center text-bold login-title">文档协作编辑系统</div>
    <div class="login-right flex flex-col">
      <!-- <div class="text-center mb-20">WELCOME</div> -->
      <el-form :model="form" :rules="rules" ref="ruleForm" class="login-form">
        <el-form-item class="login-input flex" prop="name">
          <el-icon class="flex1">
            <User />
          </el-icon>
          <el-input
            class="login-input-right"
            v-model="form.username"
            placeholder="请输入账号"
            @keyup.enter="onSubmit"
          />
        </el-form-item>
        <el-form-item class="login-input flex" prop="password">
          <el-icon class="flex1">
            <Lock />
          </el-icon>
          <el-input
            type="password"
            class="login-input-right"
            v-model="form.password"
            show-password
            clearable
            placeholder="请输入密码"
            @keyup.enter="onSubmit"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox
            class="login-remember mb-10"
            v-model="remember"
            label="记住密码"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-btn w-p-100"
            type="primary"
            @click="onSubmit()"
            >立即登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import router from "@/router";
import { ref, reactive } from "vue";
import { getDynamicRoutes } from "@/utils/getRouteList.js";
import { getAuthRouters } from "@/router/authRouter.js";
import { useAuthRouterStore } from "@/stores/authRouter.js";
import useUserStore from "@/stores/user.js";
import {
  ElNotification,
  ElMessageBox,
  ElMessage,
  ElLoading,
} from "element-plus";
const authRouterStore = useAuthRouterStore();
const userStore = useUserStore();
const ruleForm = ref(null);
const form = reactive({
  username: "",
  password: "",
});

const remember = ref(false);

const rules = {
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const onSubmit = () => {
  ruleForm.value.validate((valid) => {
    if (valid) {
      userStore.login({ ...form }).then(async (res) => {
        console.log(res);
        if (res.code == 200) {
          // 其他登录逻辑
          // 成功后将token存到session
          // sessionStorage.token = "111";
          sessionStorage.token = res.data.token;
          // 获取动态路由
          let dynamicRoutes = await getDynamicRoutes();
          // 动态添加路由
          let newRoutes = getAuthRouters(dynamicRoutes);
          authRouterStore.addRouterList(newRoutes);
          newRoutes.forEach((val) => {
            router.addRoute(val);
          });
          router.push("/home");
        }
      });
    }
  });
};
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  background: url("../../../public/img/login.png") no-repeat;
  background-size: cover;
  flex-direction: column;
}
.login-title {
  margin-top: 130px;
  font-size: 50px;
  letter-spacing: 20px;
}
.login-left {
  width: 58%;
  height: 100%;
}
.login-right {
  /* width: 25%; */
  width: 30%;
  /* height: 56%; */
  height: 40%;
  background-color: #fff;
  margin: auto;
  border-radius: 10px;
  box-shadow: #b3d2fb 1px 1px 15px 1px;
  padding: 40px 30px;
}
.login-form {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
}
.login-input {
  height: 50px;
  border: 1px solid #b3d2fb;
  border-radius: 5px;
  background: rgba(231, 241, 253, 0.4);
}
.login-input-right {
  width: 80%;
  height: 60%;
  border-left: 1px solid #ccc;
}
.login-remember {
  font-size: 12px;
}
.login-btn {
  height: 40px;
}
</style>
<style>
.login-input-right .el-input__wrapper {
  border: none !important;
  box-shadow: none !important;
  background: transparent;
}
.login-remember .el-checkbox__label {
  font-size: 12px;
  color: #aaa;
}
</style>
